//
//  STMSettingViewController.m
//  StoreManagement
//
//  Created by centling on 16/5/31.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMSettingViewController.h"
#import "STMUserInfoViewController.h"

@interface STMSettingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSArray *cellTitleArray;

@end

@implementation STMSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.cellTitleArray = @[@"用户信息修改", @"关于"];
    self.mainTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.mainTableView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self buildTableFooterView];
    [self.view addSubview:self.mainTableView];
}


- (void)buildTableFooterView {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 80)];
    UIButton *logoutButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 30, SCREEN_W-80, 40)];
    logoutButton.backgroundColor = MAINORANGECOLOR;
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    logoutButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:logoutButton];
    self.mainTableView.tableFooterView = footerView;
}

- (void)logout {
    [STMNetworkAPI logoutWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                      sellerName:[UserDefaultHandler getUserDetaultString:kSellerName]
                          client:@"ios"
                         success:^(STMLoginModel *result) {
                             if ([result.code isEqualToString:@"200"]) {
                                 [UserDefaultHandler initUserDefault];
                             }
                             UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                             STMLoginViewController *loginVC = [[STMLoginViewController alloc] init];
                             [nav presentViewController:loginVC animated:YES completion:nil];
                             [nav popToRootViewControllerAnimated:NO];
                         }
                         failure:^(NSError *err) {
                             [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                         }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    cell.textLabel.text = self.cellTitleArray[indexPath.section];
    cell.textLabel.textColor = [UIColor darkGrayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        STMUserInfoViewController *userInfoVC = [[STMUserInfoViewController alloc] init];
        [userInfoVC customNavigationTitle:@"个人信息"];
        [self.navigationController pushViewController:userInfoVC animated:YES];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 13;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
